/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc> 
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation. You may not use, modify
or distribute this program under any other version of the
GNU Affero General Public License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package server.shops;

import client.MapleCharacter;
import client.MapleClient;
import client.inventory.IItem;
import client.inventory.ItemFlag;
import server.MapleInventoryManipulator;
import tools.packet.PlayerShopPacket;

import java.util.ArrayList;
import java.util.List;

public class MaplePlayerShop extends AbstractPlayerStore {

  private int boughtnumber = 0;
  private List<String> bannedList = new ArrayList<String>();

  public MaplePlayerShop(MapleCharacter owner, int itemId, String desc) {
    super(owner, itemId, desc, "", 3);
  }

  @Override
  public void buy(MapleClient c, int item, short quantity) {
    MaplePlayerShopItem pItem = items.get(item);
    if (pItem.bundles > 0) {
      IItem newItem = pItem.item.copy();
      newItem.setQuantity((short) (quantity * newItem.getQuantity()));
      byte flag = newItem.getFlag();

      if (ItemFlag.KARMA_EQ.check(flag)) {
        newItem.setFlag((byte) (flag - ItemFlag.KARMA_EQ.getValue()));
      } else if (ItemFlag.KARMA_USE.check(flag)) {
        newItem.setFlag((byte) (flag - ItemFlag.KARMA_USE.getValue()));
      }
      final int gainmeso = pItem.price * quantity;
      if (c.getPlayer().getMeso() >= gainmeso) {
        if (getMCOwner().getMeso() + gainmeso > 0 && MapleInventoryManipulator.checkSpace(c, newItem.getItemId(), newItem.getQuantity(), newItem.getOwner()) && MapleInventoryManipulator.addFromDrop(c, newItem, false)) {
          pItem.bundles -= quantity;
          bought.add(new BoughtItem(newItem.getItemId(), quantity, gainmeso, c.getPlayer().getName()));
          c.getPlayer().gainMeso(-gainmeso, false);
          getMCOwner().gainMeso(gainmeso, false);
          if (pItem.bundles <= 0) {
            boughtnumber++;
            if (boughtnumber == items.size()) {
              closeShop(false, true);
              return;
            }
          }
        } else {
          c.getPlayer().dropMessage(1, "Your inventory is full.");
        }
      } else {
        c.getPlayer().dropMessage(1, "You do not have enough mesos.");
        //}
      }
      getMCOwner().getClient().getSession().write(PlayerShopPacket.shopItemUpdate(this));
    }
  }

  @Override
  public byte getShopType() {
    return IMaplePlayerShop.PLAYER_SHOP;
  }

  @Override
  public void closeShop(boolean saveItems, boolean remove) {
    MapleCharacter owner = getMCOwner();
    removeAllVisitors(10, 1);
    getMap().removeMapObject(this);

    for (MaplePlayerShopItem items : getItems()) {
      if (items.bundles > 0) {
        IItem newItem = items.item.copy();
        newItem.setQuantity((short) (items.bundles * newItem.getQuantity()));
        if (MapleInventoryManipulator.addFromDrop(owner.getClient(), newItem, false)) {
          items.bundles = 0;
        } else {
          saveItems(); //O_o
          break;
        }
      }
    }
    owner.setPlayerShop(null);
    update();
    getMCOwner().getClient().getSession().write(PlayerShopPacket.shopErrorMessage(10, 1));
  }

  public void banPlayer(String name) {
    if (!bannedList.contains(name)) {
      bannedList.add(name);
    }
    for (int i = 0; i < 3; i++) {
      MapleCharacter chr = getVisitor(i);
      if (chr.getName().equals(name)) {
        chr.getClient().getSession().write(PlayerShopPacket.shopErrorMessage(5, 1));
        chr.setPlayerShop(null);
        removeVisitor(chr);
      }
    }
  }

  public boolean isBanned(String name) {
    if (bannedList.contains(name)) {
      return true;
    }
    return false;
  }
}
